<style>
th span{white-space:normal;line-height:19px;}
.layui-table-cell {overflow-x: hidden;overflow: inherit;line-height:19px; padding: 3px 0px;text-align:center}
.layui-table-box {
    overflow: inherit;
}
.redclass{color:#CC0000;font-weight:bold;}
.layui-table-body {
    overflow: inherit;
}
.layui-form-item .layui-input-inline{width:200px;}
.search_form .layui-input-inline{width:120px;}
.layui-form-pane .layui-form-label {width: 100px;padding:6px 9px;}
.fixed-bar .layui-input {width:120px;}
.layui-table-view .layui-table td, .layui-table-view .layui-table th{padding:1px 0px}
</style>
<div class="fixed-bar" style="clear:both;">

    <form class="layui-form layui-form-pane fixed-bar-form" action="{:url('',['status' => $status,'process_stage' => $process_stage])}" method="get" id="hisi-table-search" >
    	<input type="hidden" name="process_stage" value="{:$process_stage}" />

        <div class="layui-form-item search_form" style="margin-bottom: 0px;">
            <a href="javascript:void(0)" id="addDetail" class="layui-btn addBills" data-url="{:url('addBills')}" lay-filter="demo1" onfocus="this.blur()">添加一个加工单</a>
        	<div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_name" lay-verify="" placeholder="输入姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">工序</label>
                <div class="layui-input-inline">
                    <input type="text" name="process_name" lay-verify="" placeholder="工序名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订单号</label>
                <div class="layui-input-inline">
                    <input type="text" name="client_ordernum" lay-verify="" placeholder="输入订单号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" id="seacher" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
                <!--                <button type="button" class="layui-btn layui-btn-primary hisi-export show">导出</button>-->
            </div>
        </div>
	</form>
</div>
<table id="dataTable" lay-filter="filterTableSFD"></table>

{include file="system@block/layui" /}
<div id="defectiveTpl" class="hide">
	<form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
	<div class="layui-form-item pt20 pl20">
    	<input type="hidden" name="process_stage" value="{:$process_stage}" />
        <input type="hidden" name="employee_is_along" class="employee_is_along" value="1" />
    	<div class="layui-inline">
			<label class="layui-form-label user_member_desc">员工编号：</label>
			<div class="layui-input-inline">
				<input type="text" name="user_number" value="" style="width:110px;display:inline;" placeholder="输入员工编号" class="layui-input user_number_input">
                <span class="employeeSetTag"><strong onclick="setEmployeeType(0)">是临时工？</strong></span>
			</div>
		</div>
        <div class="layui-inline">
			<label class="layui-form-label">订 单 号：</label>
			<div class="layui-input-inline">
				<input type="text" name="client_ordernum" value="" style="width:150px;display:inline;" placeholder="加工订单号" class="layui-input">&nbsp;
			</div>
		</div>
        <div class="layui-inline">
			<label class="layui-form-label">加工工序：</label>
			<div class="layui-input-inline">
				<input type="text" name="process_name" value="" style="width:110px;display:inline;" placeholder="加工工序" class="layui-input process_name_input">
								{:Form::select('delivery_status', '', ['' => '可选工序']+$processNameList, '', '','lay-filter="storehouse-id" onchange="setProcessName(this)" lay-ignore')}
			</div>
		</div>
        <div class="layui-inline">
			<label class="layui-form-label">加工数量：</label>
			<div class="layui-input-inline">
				<input type="text" name="quintity" value="" style="width:150px;display:inline;" placeholder="正数增加负数扣除" class="layui-input">&nbsp;
			</div>
		</div>
        <div class="layui-inline">
			<label class="layui-form-label">加工单价：</label>
			<div class="layui-input-inline">
				<input type="text" name="process_price" value="" style="width:150px;display:inline;" placeholder="工序单价" lay-verify="required" class="layui-input">&nbsp;
			</div>
		</div>
        <div class="layui-inline">
			<label class="layui-form-label">薪资月份：</label>
			<div class="layui-input-inline">
				<input type="text" name="work_time" value="{$today}" style="width:150px;display:inline;" placeholder="将对应到结算月份" class="layui-input work_time_input">&nbsp;
			</div>
		</div>
        <div class="layui-inline factory_id_select" style="display:none;">
			<label class="layui-form-label">所属工厂：</label>
			<div class="layui-input-inline">
				{:Form::select('factory_name', '', $factoryKV, '', '','class="factory_id_select"')}
			</div>
		</div>
        <div style="margin-top:5px;color:#666666;margin-bottom:15px;">注：已经增加过的临时工姓名，再次输入其其它单加工工序时，请输入系统对其生成的编号，不要再作为一个新的临时工数据输入</div>
		<input type="hidden" name="url" value="{{ d.url }}">
		<div class="" style="text-align:center">
          <button class="layui-btn" lay-submit lay-filter="stock-defective" >确认</button>
        </div>
	</div>
	</form>
</div>
<script type="text/html" id="stallOrderTimeTpl">
    <input type="text" name="work_time" readonly="" style="width: 90px;margin-left:10px;" id="work_time{{d.id}}"
           value="{{layui.util.toDateString(d.work_time * 1000, 'yyyy-MM-dd')}}" lay-verify="" class="layui-input stall_order_time" lay-event="stall_order_time" style="border: none">
</script>
<script type="text/html" id="usernameTpl">
    <span class="copy-item">{{ d.user_name }}</span> <br>
    <span class="copy-item">{{ d.user_number }}</span>
</script>
<script type="text/html" id="subbtnTpl">
    {{# if(d.check_state <= 1){ }}
        <a class="layui-btn layui-btn-xs subbtn-{{ d.id }}" lay-event="save">确认</a>
		<a class="layui-btn layui-btn-xs layui-btn-danger" style="margin-left:5px;" lay-event="delete">删除</a>
    {{# }else if(d.check_state == 2 || d.check_state == 3){ }}
       已审核
    {{# } }}

</script>
<script type="text/html" id="caigouUnitPriceTpl">
    {{# if(parseFloat(d.caigou_unitprice) > parseFloat(d.customer_price)){ }}  
        <span style="color:red">{{d.caigou_unitprice}}</span>
    {{# }else{ }}
        {{d.caigou_unitprice}}
    {{# } }}    
</script>
<script type="text/javascript">
    layui.use(['opTable','table','jquery', 'tool','laytpl', 'form','laydate', 'layer'], function() {
        var $ = layui.jquery;
        var table = layui.table;
        var laytpl=layui.laytpl;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
		form.render();
        
        laydate.render({
		    elem: '#caigou_confirm_time'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#caigou_confirm_time').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
        var opTable=table.render({
            elem: '#dataTable'
            ,url: "{:url('editStatements')}?process_stage={$process_stage}" //数据接口
            ,page: true //开启分页
            ,skin: 'row'
            ,even: true
            ,limit: 10
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
            ,cols: [[ //表头
                {field:'work_time',title : '完成日期',templet: '#stallOrderTimeTpl',width:120}
                ,{field: 'user_name', title: '姓名/编号',templet:'#usernameTpl'}
                ,{field:'client_ordernum',title : '订单号',width:140}
				,{field: 'quantity', title: '订单总数'}
                ,{field: 'shicai_count', title: '实裁数',templet:function(d){ return d.shicai_count;}}
				,{field: 'process_name', title: '工序'}
                ,{field:'process_quintity',title : '加工数量'}
                ,{field:'process_count',title : '累计加工数',templet:function(d){ var classval=Number(d.process_count)>Number(d.shicai_count)? 'redclass' :'';
                        return "<span class='"+classval+"'>"+d.process_count+"</span>";}}
                ,{field:'tot_quintity',title : '结算数量',edit:'text',style:'border-bottom:2px dotted #5FB878;',
				templet:function(d){ var classval=Number(d.process_count)>Number(d.shicai_count)? 'redclass' :'';
				return d.tot_quintity;}}
                ,{field:'process_price',title : '单价',edit:'text',style:'border-bottom:2px dotted #5FB878;'}
                ,{field:'money_total',title : '结算金额',templet:function(d){return "<span id='money_total"+d.id+"'>"+d.money_total+"</span>";}}
				,{field: 'creater_name', title: '录入人'}
               ,{field:'subbtn', title:'确认',width:100, templet:'#subbtnTpl'}
            ]]
            ,done: function (res) {
				var data=res.data,mInfo=res.mInfo;
                for (var i in data){
                    var value=layui.util.toDateString(data[i]['work_time'] * 1000, 'yyyy-MM-dd');
					var nowtime=new Date();
                    if(data[i]['work_time']==0) value=nowtime.getFullYear()+"-"+(nowtime.getMonth()+1)+"-01";
                    laydate.render({
                        elem: "#work_time"+data[i]['id'], //指定元素
                        value: value 
                    });
                    $('#stall-ids-'+data[i]['id']).val(data[i]['stall_id']);
                }
            }
        });
        table.on('edit(filterTableSFD)', function(obj){
            var value = obj.value //得到修改后的值
                ,d = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            var caigouUnitprice=0
            if(field == 'tot_quintity' || field == 'process_price'){
                tot_quintity=d.tot_quintity;
                var caigou_amount = layui.tool.bcmul(d.tot_quintity,d.process_price);
                obj.update({money_total:caigou_amount});
            }
        });
        table.on('tool(filterTableSFD)', function(obj){
            var d = obj.data,field=obj.field;
            if(obj.event === 'save' || obj.event === 'delete'){
                if(obj.event==='delete'){
                    if(confirm('确定要删除该入单信息吗')==false){
                        return;
                    }
                }
                var id=d.id;
                //var stallId = $("#stall-ids-"+id).val();
                var tot_quintity = d.tot_quintity;
                var process_price= d.process_price;
                var work_time=$("#work_time"+id).val();

                console.log(work_time);
                console.log(d.tot_quintity);
                console.log(d.process_price);

                if(!checkData(d)) return;
                $.ajax({
                    type: "POST",
                    url: "{:url('addBills')}",
                    data: {
                        id:id,
                        order_id:d.order_id,
                        work_time:work_time,
                        quintity:tot_quintity,
                        process_price:process_price,
                        action:obj.event
                    },
                    dataType:"json",
                    success: function(res){
                        if (res.code == 1) {
                            $('.money_total'+id).html(tot_quintity*process_price);
                            layer.msg(res.msg, {icon: 1});
                            if(obj.event === 'delete'){
                                obj.tr[0].remove();
                            }else{
                                //setTimeout("parent.layui.layer.closeAll()",200);
                            }
                            //parent.reloadTable();
                        } else {
                            layer.msg(res.msg, {icon: 2,anim: 6});
                        }
                    }
                });
            }

        });
        var layerDefective = null;
        $(document).on('click','#addDetail',function(){
            var orderId = $(this).attr('data-id');
            var url = $(this).attr('data-url');
            var json = $(this).attr('data-json');
            var in_store_count=$(this).attr('in_store_count');
            var d = [];
            d['list'] = eval("(" + json+ ")");
            d['url'] = url;
            d['order_id'] = orderId;
            d['in_store_count']=in_store_count;
            var content = laytpl($('#defectiveTpl').html()).render(d);
            layerDefective = layer.open({
                type: 1
				,area: ['700px','450px']
                ,shade: 0
                ,offset: '100px'
                ,anim: parseInt(Math.random()*(6+1),10)
                ,skin:'layui-layer-molv'
                ,title:'增加一个工价单'
                ,content: content
            });
			form.render();
			$(".user_number_input").eq(1).focus();
			
        });
        form.on('submit(stock-defective)', function (data) {
            console.log(data.field);
            var url = data.field.url;
            $.ajax({
                type: "POST",
                url: url,
                data: data.field,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        $('#defectiveTpl form')[0].reset();
                        layer.close(layerDefective);
                        layer.msg(res.msg, {icon: 1,end:function(){opTable.reload();}});
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });

            return false;
        });
		//设置加工单职员类型
    	window.setEmployeeType=function(isAlong){
			var descText=isAlong==1 ? '员工编号' : '临时工姓名：';	
			$(".user_number_input").attr('placeholder', isAlong==1 ? '员工编号' : '临时工姓名').focus();
			$(".employee_is_along").val(isAlong);
			$(".user_member_desc").html(descText);
			$(".factory_id_select").css("display", isAlong==1 ? 'none' : '');
			$(".employeeSetTag").html(isAlong==1 ? '<strong onclick="setEmployeeType(0)">是临时工？</strong></span>' : '<strong onclick="setEmployeeType(1)">是正式工？</strong></span>');
    	};
		 //新增时选择加工工序
    	window.setProcessName=function(d){
			//alert($(d).val());
			$(".process_name_input").val($(d).val())
    	};


        // 单元格修改数据校验
		window.checkData = function(d) {
			var caigouUnitprice = d.tot_quintity;
        	var billingCaigouKgprice = d.process_price;
            var stallFeedType = d.stall_feed_type;
        	var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
        	if (!reg.test(caigouUnitprice) || d.tot_quintity<=0) {
        		layer.msg('加工数量输入有误', {icon: 2,anim: 6});
        		return false;
        	}
        	if (!reg.test(billingCaigouKgprice)) {
        		layer.msg('工序单价输入有误', {icon: 2,anim: 6});
        		return false;
        	}
        	return true;
		}

    });
</script>